package dao

import (
	"live/inits"
	"live/model"
)

func Register(name string, password string, money int) (user model.User, err error) {
	user = model.User{
		Name:     name,
		Password: password,
		Money:    money,
	}
	err = inits.DB.Create(&user).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil
}

func Login(name string, password string) (user model.User, err error) {
	err = inits.DB.Where("name = ?", name).Where("password = ?", password).Find(&user).Limit(1).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil
}

func Where(name string) (user model.User, err error) {
	err = inits.DB.Where("name = ?", name).Find(&user).Limit(1).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil
}

func AddRoom(name string, brief string, describe string) (room model.LiveRoom, err error) {
	room = model.LiveRoom{
		Name:     name,
		Brief:    brief,
		Describe: describe,
	}
	err = inits.DB.Create(&room).Error
	if err != nil {
		return model.LiveRoom{}, err
	}
	return room, nil
}

func ListRoom(id uint64) (room model.LiveRoom, err error) {
	err = inits.DB.Where("id = ?", id).Find(&room).Limit(1).Error
	if err != nil {
		return model.LiveRoom{}, err
	}
	return room, nil
}

func UpdateRoom(id uint64, brief string) bool {
	err := inits.DB.Model(&model.LiveRoom{}).Where("id = ?", id).Update("brief", brief).Error
	if err != nil {
		return false
	}
	return true
}

func DeleteRoom(id uint64) bool {

	err := inits.DB.Debug().Where("id  = ?", id).Delete(&model.LiveRoom{}).Error
	if err != nil {
		return false
	}
	return true
}

func AddComment(id uint, comment string, id2 int64) (commentForm model.CommentForm, err error) {
	commentForm = model.CommentForm{
		UserId:  uint64(id),
		Comment: comment,
		RoomId:  id2,
	}
	err = inits.DB.Create(&commentForm).Error
	if err != nil {
		return model.CommentForm{}, err
	}
	return commentForm, nil
}

func ListComment(id int64) (commentFrom []model.CommentForm, err error) {
	err = inits.DB.Where("room_id = ?", id).Find(&commentFrom).Error
	if err != nil {
		return []model.CommentForm{}, err
	}
	return commentFrom, nil
}

func UpdateComment(id uint64, comment string) interface{} {
	err := inits.DB.Model(&model.CommentForm{}).Where("id = ?", id).Update("comment", comment).Error
	if err != nil {
		return false
	}
	return true
}

func DeleteComment(id uint64) interface{} {
	err := inits.DB.Where("id  = ?", id).Delete(&model.CommentForm{}).Error
	if err != nil {
		return false
	}
	return true
}

func UserWhere(id int64) (user model.User, err error) {
	err = inits.DB.Where("id = ?", id).Find(&user).Limit(1).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil
}

func Usermoney(id uint) (user model.User, err error) {
	err = inits.DB.Where("id = ?", id).Find(&user).Limit(1).Error
	if err != nil {
		return model.User{}, err
	}
	return user, nil
}

func Usergift(id int64) (gift model.Gift, err error) {
	err = inits.DB.Where("id = ?", id).Find(&gift).Limit(1).Error
	if err != nil {
		return model.Gift{}, err
	}
	return gift, nil
}

func UpdateUser(id int, jmoney int) bool {
	err := inits.DB.Model(&model.User{}).Where("id = ?", id).Update("money", jmoney).Error
	if err != nil {
		return false
	}
	return true
}

func UpdatefUser(id int, fmoney int) bool {
	err := inits.DB.Model(&model.User{}).Where("id = ?", id).Update("money", fmoney).Error
	if err != nil {
		return false
	}
	return true
}

func AddReceive(id uint, notes string, id2 int64, id3 int64, id4 int64) (receive model.ReceiveGift, err error) {
	receive = model.ReceiveGift{
		UserId:    int64(id),
		GiftId:    id4,
		Notes:     notes,
		ReceiveId: id3,
		RoomId:    id2,
	}
	err = inits.DB.Create(&receive).Error
	if err != nil {
		return model.ReceiveGift{}, err
	}
	return receive, nil

}

func ListReceive(id int64) (receive []model.ReceiveGift, err error) {
	err = inits.DB.Where("room_id = ?", id).Find(&receive).Error
	if err != nil {
		return []model.ReceiveGift{}, err
	}
	return receive, nil
}

func UpdateReceive(id uint64, notes string) bool {
	err := inits.DB.Model(&model.ReceiveGift{}).Where("id = ?", id).Update("notes", notes).Error
	if err != nil {
		return false
	}
	return true
}

func DeleteReceive(id uint64) bool {
	err := inits.DB.Where("id  = ?", id).Delete(&model.ReceiveGift{}).Error
	if err != nil {
		return false
	}
	return true
}
